package academic.concordia.bankofquebec.bean.mutualfunds;

import java.util.List;

import javax.faces.bean.ManagedProperty;

import org.springframework.transaction.annotation.Transactional;

import academic.concordia.bankofquebec.bean.bankAccount.IBankAccountServices;
import academic.concordia.bankofquebec.dao.client.IClientDAO;
import academic.concordia.bankofquebec.entity.AccountAttributes;
import academic.concordia.bankofquebec.entity.BankAccountStatus;
import academic.concordia.bankofquebec.entity.CheckingsAccount;
import academic.concordia.bankofquebec.entity.Client;
import academic.concordia.bankofquebec.entity.MutualFunds;
import academic.concordia.bankofquebec.entity.SavingsAccount;
import academic.concordia.bankofquebec.entity.Transaction;
import academic.concordia.bankofquebec.entity.TransactionStatus;

@Transactional(readOnly = true)
public class MutualFundsBean implements IMutualFundsBean {

	private IBankAccountServices bankAccountBean;
	
	private IClientDAO clientDAO;

	@Override
	public List<Transaction> getLatestMonthStatement(MutualFunds mutualFunds) {
		// TODO Auto-generated method stub
		return null;
	}

	@Transactional(readOnly = false)
	@Override
	public void transferInitialInvestment(MutualFunds mutualFunds,
			CheckingsAccount checkingsAccount) {
		Transaction transferInvestment = bankAccountBean.transfer(checkingsAccount, mutualFunds, mutualFunds.getInvestedAmount());
		if (transferInvestment.isApproved()){
			mutualFunds.setStatus(BankAccountStatus.ACTIVATED);
			clientDAO.updateClient(mutualFunds.getHolder());
		}
	}

	@Override
	@Transactional(readOnly = false)
	public boolean createMutualFund(Client client, MutualFunds mutualFunds) {
		if (client.hasProduct(AccountAttributes.CHECKINGS_ACCOUNT.name())){
			CheckingsAccount checkingsAccount = new CheckingsAccount();
			checkingsAccount = new CheckingsAccount();
			bankAccountBean.addAccount(checkingsAccount, client);
			SavingsAccount savingsAccount = new SavingsAccount();
			bankAccountBean.addAccount(savingsAccount, client);
		}
			
		return bankAccountBean.addAccount(mutualFunds, client);
	}

	public IClientDAO getClientDAO() {
		return clientDAO;
	}

	public void setClientDAO(IClientDAO clientDAO) {
		this.clientDAO = clientDAO;
	}

	public IBankAccountServices getBankAccountBean() {
		return bankAccountBean;
	}

	public void setBankAccountBean(IBankAccountServices bankAccountBean) {
		this.bankAccountBean = bankAccountBean;
	}
	
	

}
